package com.woniuxy.dao;

import com.woniuxy.entity.OneCard;
import com.woniuxy.utils.ConnectionUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

public class OneCardDao {
    public void createCard(OneCard oneCard) {
        String sql = "insert into onecard (cardnum,userid,balance,createtime,status) values (?,?,?,NOW(),0)";
        Connection connection = ConnectionUtil.getConnection();
        try {
            PreparedStatement preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1, oneCard.getCardNumber());
            preparedStatement.setInt(2, oneCard.getUserid());
            preparedStatement.setDouble(3,oneCard.getBalance());
            preparedStatement.executeUpdate();
            System.out.println("一卡通创建成功!");
        } catch (SQLException throwables) {
            System.out.println("一卡通创建失败，失败原因是"+throwables.getMessage());
            throwables.printStackTrace();
        }finally {
            try {
                connection.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
    }
}
